package com.tyros.web.log.aspect;

import com.tyros.web.log.HandlerLogProperties;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

/**
 * @author zhangshuaiyin
 * @date 2022/7/2 11:04
 */
@Slf4j
@Aspect
public class ControllerAnnotationAspect {
    private final HandlerLogProperties properties;

    public ControllerAnnotationAspect(HandlerLogProperties properties) {
        this.properties = properties;
    }

    @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping) " +
            "|| @annotation(org.springframework.web.bind.annotation.PostMapping)" +
            "|| @annotation(org.springframework.web.bind.annotation.RequestMapping)" +
            "|| @annotation(org.springframework.web.bind.annotation.DeleteMapping)" +
            "|| @annotation(org.springframework.web.bind.annotation.PutMapping)" +
            "|| @annotation(org.springframework.web.bind.annotation.PatchMapping)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
        return AspectLogHelper.successLog(joinPoint, null, properties);
    }

    @AfterThrowing(throwing = "ex", pointcut = "pointCut()")
    public Object throwing(Throwable ex) {
        AspectLogHelper.throwingLog(ex, null, properties);
        return null;
    }
}
